package com.wanmait.recruit.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.PersonSystemInfoMapper;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.pojo.PersonSystemInfo;
import com.wanmait.recruit.service.PersonSystemInfoService;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("personSystemInfoService")
@Transactional(rollbackFor = Exception.class)
public class PersonSystemInfoServiceImpl implements PersonSystemInfoService {

    @Resource
    PersonSystemInfoMapper personSystemInfoMapper;

    @Override
    public void insert(PersonSystemInfo object) {
        personSystemInfoMapper.insertSelective(object);
    }

    @Override
    public void delete(PersonSystemInfo object) {

    }

    @Override
    public void update(PersonSystemInfo object) {
        personSystemInfoMapper.updateByPrimaryKeySelective(object);
    }

    @Override
    public PageInfo<PersonSystemInfo> search(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public PageInfo<PersonSystemInfo> findAll(Person person) {
        PageHelper.startPage(PageContext.getPageNum(),1);
        List<PersonSystemInfo> personSystemInfos = personSystemInfoMapper.findAll(person.getId());

        PageInfo<PersonSystemInfo> pageInfo = new PageInfo<>(personSystemInfos);
        pageInfo.getList().forEach(personSystemInfo -> {
            Date date = personSystemInfo.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = simpleDateFormat.format(date);
            personSystemInfo.setTimeStr(time);
        });
        return pageInfo;
    }

    @Override
    public PersonSystemInfo findById(Integer id) {
        PersonSystemInfo personSystemInfo = personSystemInfoMapper.selectByPrimaryKey(id);
        Date date = personSystemInfo.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        personSystemInfo.setTimeStr(time);
        return personSystemInfo;
    }
}
